﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutomaTones.Model.Automaton_Model;
using Midi;
using Microsoft.Pex.Framework;

namespace AutomaTones.Model.MIDI.Mappers {
    [PexClass]
    partial class HqstTranslator : IMapper
    {
        private readonly List<NoteOut> _notes = new List<NoteOut>();
        [PexMethod]
        public List<NoteOut> Translate(State[,] grid)
        {
            _notes.Clear();
            if (grid.GetLength(0) < 3 || grid.GetLength(1) < 3)
                return _notes;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++) {
                    if (grid[i, j].CurrentState == 0)
                        continue;
                    switch (grid[i, j].CurrentState) {
                        case 1:
                            {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 1, 50,
                                                       grid[i, j].CurrentState % 3));
                                break;
                            }
                       case 2:
                            {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 2, 50,
                                                       grid[i, j].CurrentState % 3));
                                break;
                            }
                      case 3:
                            {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 3, 50,
                                                       grid[i, j].CurrentState % 3));
                                break;
                            }
                      case 4:
                            {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 4, 50,
                                                       grid[i, j].CurrentState % 4));
                                break;
                            }
                        case 5:
                            {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 4, 50,
                                                       grid[i, j].CurrentState % 5));
                                break;
                            }
                       case 6:
                            {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 1, 50,
                                                       grid[i, j].CurrentState % 6));
                                break;
                            }
                      case 7:
                            {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 1, 50,
                                                       grid[i, j].CurrentState % 7));
                                break;
                            }
                    }
                }
            }

            for (int i = grid.GetLength(0)-3; i < grid.GetLength(0); i++) {
                for (int j = grid.GetLength(1) - 3; j < grid.GetLength(1); j++) {
                    if (grid[i, j].CurrentState == 0)
                        continue;
                    switch (grid[i, j].CurrentState) {
                        case 1: {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 1, 50,
                                                       grid[i, j].CurrentState % 3));
                                break;
                            }
                        case 2: {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 2, 50,
                                                       grid[i, j].CurrentState % 3));
                                break;
                            }
                        case 3: {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 3, 50,
                                                       grid[i, j].CurrentState % 3));
                                break;
                            }
                        case 4: {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 4, 50,
                                                       grid[i, j].CurrentState % 4));
                                break;
                            }
                        case 5: {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 4, 50,
                                                       grid[i, j].CurrentState % 5));
                                break;
                            }
                        case 6: {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 1, 50,
                                                       grid[i, j].CurrentState % 6));
                                break;
                            }
                        case 7: {
                                _notes.Add(new NoteOut(Pitch.A3, grid[i, j].CurrentState * 1, 50,
                                                       grid[i, j].CurrentState % 7));
                                break;
                            }
                    }
                }
            }
            return _notes;
        }
    }
}
